/*
 * @Author: wangyongcun
 * @Date: 2023-09-06 10:50:08
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-06-12 08:49:24
 * @Description: 技术需求
 */
class Demand {
  constructor() {}
  /**
   * @description: 添加技术需求
   * @param {*} ctx
   * @return {*}
   */
  async addDemand(ctx) {
    try {
      // 获取参数
      let isEmpty = reqUtil.isEmpty(["name", "phone", "content"], ctx);
      if (isEmpty) return;
      // 查询数据是否已存在
      // 添加到数据库
      let rowBody = ctx.request.body;
      let rowBodyKeys = Object.keys(rowBody);
      let sqlParams = `id='${uuidv4()}',`;
      rowBodyKeys.forEach((x, i) => {
        sqlParams += `${x}='${rowBody[x]}'${
          i == rowBodyKeys.length - 1 ? "" : ","
        }`;
      });
      let sql = `INSERT INTO demand SET ${sqlParams}`;
      let { status } = await db(sql);
      if (status != 200) throw "添加技术需求失败";
      // 返回结果
      ctx.body = resUtil.success("添加成功");
    } catch (err) {
      log.error(`Demand-addDemand-添加技术需求失败：${err}`);
      ctx.body = resUtil.error(err);
    }
  }
  /**
   * @description: 更新技术需求
   * @param {*} ctx
   * @return {*}
   */
  async updateDemand(ctx) {
    try {
      // 获取参数
      let isEmpty = reqUtil.isEmpty(["id"], ctx);
      if (isEmpty) return;
      // 查询数据是否已存在
      // 添加到数据库
      let rowBody = ctx.request.body;
      let rowBodyKeys = Object.keys(rowBody);
      let sqlParams = "";
      rowBodyKeys.forEach((x, i) => {
        sqlParams += `${x}='${rowBody[x]}'${
          i == rowBodyKeys.length - 1 ? "" : ","
        }`;
      });
      let sql = `UPDATE demand SET ${sqlParams} WHERE id='${rowBody.id}'`;
      let { status, results } = await db(sql);
      if (status != 200 || results.affectedRows == 0) throw "更新技术需求失败";
      // 返回结果
      ctx.body = resUtil.success("更新成功");
    } catch (err) {
      log.error(`Demand-updateDemand-更新失败：${err}`);
      ctx.body = resUtil.error(err);
    }
  }
  /**
   * @description: 删除技术成果
   * @param {*} ctx
   * @return {*}
   */
  async deleteDemand(ctx) {
    try {
      // 获取参数
      let isEmpty = reqUtil.isEmpty(["id"], ctx);
      if (isEmpty) return;
      // 添加到数据库
      let rowBody = ctx.request.body;
      let sql = `DELETE FROM demand WHERE id='${rowBody.id}'`;
      let { status, results } = await db(sql);
      if (status != 200 || results.affectedRows == 0) throw "未找到该技术需求";
      // 返回结果
      ctx.body = resUtil.success("删除成功");
    } catch (err) {
      log.error(`Demand-deleteDemand-删除失败：${err}`);
      ctx.body = resUtil.error(err);
    }
  }
  /**
   * @description: 查询技术成果
   * @param {*} ctx
   * @return {*}
   */
  async queryDemand(ctx) {
    try {
      // 获取参数
      let isEmpty = reqUtil.isEmpty(["id"], ctx);
      if (isEmpty) return;
      // 添加到数据库
      let rowBody = ctx.request.body;
      let sql = `SELECT * FROM demand WHERE id='${rowBody.id}'`;
      let { status, results } = await db(sql);
      console.log(results, "results");
      if (status != 200 || results.length == 0) throw "未找到该技术需求";
      // 返回结果
      ctx.body = resUtil.success({ Content: results[0] });
    } catch (err) {
      log.error(`Demand-queryDemand-查询失败：${err}`);
      ctx.body = resUtil.error(err);
    }
  }
  /**
   * @description: 查询科技成果列表
   * @param {*} ctx
   * @return {*}
   */
  async queryDemandList(ctx) {
    try {
      // 获取参数
      let isEmpty = reqUtil.isEmpty(["pageNum", "pageSize"], ctx);
      if (isEmpty) return;
      let { pageNum, pageSize, keyword } = ctx.request.body;
      let whereSql = keyword ? `WHERE name LIKE '%${keyword}%'` : "";
      // 查询总条数
      let Count = 0;
      let countSql = `SELECT COUNT(1) FROM demand ${whereSql}`;
      let { status: countStatus, results: countRes } = await db(countSql);
      if (countStatus != 200) throw "查询总条数失败";
      Count = countRes[0]["COUNT(1)"];
      // 查询列表数据
      let listSql = `SELECT * FROM demand ${whereSql} ORDER BY updateTime DESC LIMIT ${pageSize} OFFSET ${
        pageSize * (pageNum - 1)
      }`;
      let { status: listStatus, results: listRes } = await db(listSql);
      if (listStatus != 200) throw "查询技术需求失败";
      // 返回结果
      ctx.body = resUtil.success({ Content: listRes, Count });
    } catch (err) {
      log.error(`Demand-queryDemandList-查询失败：${err}`);
      ctx.body = resUtil.error(err);
    }
  }
}

export default new Demand();
